package com.bjb.vr.manager.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjb.vr.common.constant.CacheConstant;
import com.bjb.vr.common.constant.Constants;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResDictionary;
import com.bjb.vr.common.dto.ResFavorites;
import com.bjb.vr.common.mapper.ResDictionaryMapper;
import com.bjb.vr.common.mapper.ResFavoritesMapper;
import com.bjb.vr.common.utils.GuavaCacheUtil;
import com.bjb.vr.common.utils.LoadingCacheUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 初始化缓存配置
 *
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/3/28 13:01
 */
@Slf4j
@Configuration
public class CacheConfig {

    @Value("${dictionaryDuration:10}")
    private long dictionaryDuration;

    @Autowired
    private ResDictionaryMapper dictionaryMapper;

    @Autowired
    private ResFavoritesMapper favoritesMapper;


    @Bean
    public void initDictionaryCacheList() {
        // 1、缓存字典数据--多条数据
        LoadingCache<String, List<ResDictionary>> dictionaryCacheList = LoadingCacheUtil.asyncReloadMin(150L, dictionaryDuration, new CacheLoader() {

            @Override
            public Object load(Object o) {
                ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
                String dictType = (String) o;
                log.info("重新刷新字典缓存数据 >>> 缓存key-【dictType: {}】", StringUtils.isBlank(dictType) ? "all" : dictType);
                LambdaQueryWrapper<ResDictionary> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ResDictionary::getDictStatus, Constants.ENABLE);
                queryWrapper.eq(StringUtils.isNotBlank(dictType), ResDictionary::getDictType, dictType);
                return dictionaryMapper.selectList(queryWrapper);
            }
        });
        GuavaCacheUtil.setDictionaryCache(dictionaryCacheList);
    }

    @Bean
    public void initDictionaryCacheOne() {
        // 2、缓存字典数据--单条
        LoadingCache<String, ResDictionary> dictionaryCacheOne = LoadingCacheUtil.asyncReloadMin(150L, dictionaryDuration, new CacheLoader() {

            @Override
            public Object load(Object o) {
                ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
                String queryType = Splitter.on(":").splitToList(o.toString()).get(0);
                String params = Splitter.on(":").splitToList(o.toString()).get(1);
                if (queryType.equals("dicCode")) {
                    log.info("重新刷新字典缓存数据 >>> 缓存key-【dictCode: {}】", params);
                    LambdaQueryWrapper<ResDictionary> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(ResDictionary::getDictStatus, Constants.ENABLE);
                    queryWrapper.eq(StringUtils.isNotBlank(params), ResDictionary::getDictCode, params);
                    return dictionaryMapper.selectOne(queryWrapper);
                } else {
                    log.info("重新刷新字典缓存数据 >>> 缓存key-【dictName: {}】", params);
                    LambdaQueryWrapper<ResDictionary> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(ResDictionary::getDictStatus, Constants.ENABLE);
                    queryWrapper.eq(StringUtils.isNotBlank(params), ResDictionary::getDictName, params);
                    return dictionaryMapper.selectOne(queryWrapper);
                }
            }
        });
        GuavaCacheUtil.setDictionaryCacheOne(dictionaryCacheOne);
    }

    @Bean
    public void initFavoriteCache() {
        // 1、缓存字典数据--多条数据
        LoadingCache<String, List<ResFavorites>> favorityCacheList = LoadingCacheUtil.asyncReloadMin(150L, dictionaryDuration, new CacheLoader() {

            @Override
            public Object load(Object o) {
                ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
                String accountId = Splitter.on(CacheConstant.SEPARATOR).splitToList(o.toString()).get(0);
                String resId = Splitter.on(CacheConstant.SEPARATOR).splitToList(o.toString()).get(1);
                log.info("重新刷新我的收藏缓存数据 >>> 缓存key-【account_id: {}, resId:{}】", accountId, resId);
                LambdaQueryWrapper<ResFavorites> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(accountId), ResFavorites::getAccountId, accountId);
                queryWrapper.eq(StringUtils.isNotBlank(resId), ResFavorites::getResId, resId);
                return favoritesMapper.selectList(queryWrapper);
            }
        });
        GuavaCacheUtil.setFavoritesCache(favorityCacheList);
    }
}
